﻿Imports System.Collections.Generic
Imports System.Data
Imports Biz
Imports System.Data.SqlClient


''' <summary>
''' Clase de la capa de acceso a datos para el manejo de la entidad Bitacora_Usuario
''' </summary>
''' <remarks></remarks>
Public Class DALBitacora

    Private Shared m_Instance As DALBitacora
    Private Shared m_Semaforo As New Object
    Private m_TDGBitacora As TDGBitacora


    Public Function Insert(obj As cBitacora_Usuario) As Boolean
        Dim iResultado As Int16 = 0
        Dim database As cDatabase

        Try
            database = cDatabase.Instance

            database.Conectar()
            iResultado = m_TDGBitacora.Insert(database.Conexion, obj.oUsuario.Usu_Legajo, obj.oOperacion.Oper_Codigo)
            database.Desconectar()

            Return (iResultado > 0)
        Catch ex As Exception
            Throw New Excepciones.BitacoraFalloAlta(ex)
        End Try

    End Function

    Public Function SelectxPeriodo(Periodo As Utils.Enumeradores.ConsultaBitacora) As Collection
        Dim dr As SqlDataReader
        Dim colObjetos As Collection
        Dim objAux As cBitacora_Usuario
        Dim database As cDatabase


        Try
            database = cDatabase.Instance

            database.Conectar()

            Select Case Periodo
                Case Utils.Enumeradores.ConsultaBitacora.Hoy
                    dr = m_TDGBitacora.FindHoy(database.Conexion)
                Case Utils.Enumeradores.ConsultaBitacora.Ayer
                    dr = m_TDGBitacora.FindAyer(database.Conexion)
                Case Utils.Enumeradores.ConsultaBitacora.Semana
                    dr = m_TDGBitacora.Find7dias(database.Conexion)
                Case Utils.Enumeradores.ConsultaBitacora.Mes
                    dr = m_TDGBitacora.Find30dias(database.Conexion)
                Case Else
                    dr = m_TDGBitacora.FindAll(database.Conexion)
            End Select

            If dr.HasRows Then
                colObjetos = New Collection
                Do While dr.Read
                    objAux = New cBitacora_Usuario

                    objAux.oUsuario.Usu_Legajo = Utils.Convert.ToInt16(dr.GetValue(0))
                    objAux.oOperacion.Oper_Codigo = Utils.Convert.ToInt16(dr.GetValue(1))
                    objAux.Bita_FechaHora = Utils.Convert.ToDateTime(dr.GetValue(2))
                    objAux.oOperacion.Oper_Descrip = Utils.Convert.ToStr(dr.GetValue(3))
                    objAux.oUsuario.Usu_Nombre = Utils.Convert.ToStr(dr.GetValue(4))
                    objAux.oUsuario.Usu_Apellido = Utils.Convert.ToStr(dr.GetValue(5))

                    colObjetos.Add(objAux)
                Loop
                dr.Close()
            Else
                colObjetos = Nothing
            End If

            database.Desconectar()

            Return colObjetos
        Catch ex As Exception
            Throw New Excepciones.BitacoraFalloConsulta(ex)
        End Try

    End Function

    Public Function SelectPorUsuario(Usu_Legajo As Int16) As Collection
        Dim dr As SqlDataReader
        Dim colObjetos As Collection
        Dim objAux As cBitacora_Usuario
        Dim database As cDatabase

        Try
            database = cDatabase.Instance

            database.Conectar()
            dr = m_TDGBitacora.FindByUser(database.Conexion, Usu_Legajo)

            If dr.HasRows Then
                colObjetos = New Collection
                Do While dr.Read
                    objAux = New cBitacora_Usuario

                    objAux.oUsuario.Usu_Legajo = Utils.Convert.ToInt16(dr.GetValue(0))
                    objAux.oOperacion.Oper_Codigo = Utils.Convert.ToInt16(dr.GetValue(1))
                    objAux.Bita_FechaHora = Utils.Convert.ToDateTime(dr.GetValue(2))
                    objAux.oOperacion.Oper_Descrip = Utils.Convert.ToStr(dr.GetValue(3))
                    objAux.oUsuario.Usu_Nombre = Utils.Convert.ToStr(dr.GetValue(4))
                    objAux.oUsuario.Usu_Apellido = Utils.Convert.ToStr(dr.GetValue(5))

                    colObjetos.Add(objAux)
                Loop
                dr.Close()
            Else
                colObjetos = Nothing
            End If

            database.Desconectar()

            Return colObjetos
        Catch ex As Exception
            Throw New Excepciones.BitacoraFalloConsulta(ex)
        End Try
    End Function

    Public Shared Function Instance() As DALBitacora
        If m_Instance Is Nothing Then
            SyncLock m_Semaforo
                If m_Instance Is Nothing Then
                    m_Instance = New DALBitacora
                End If
            End SyncLock
        End If

        Return m_Instance
    End Function

    Private Sub New()
        m_TDGBitacora = TDGBitacora.Instance()
    End Sub

End Class
